package com.lagou.filter;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@WebFilter(filterName = "UserFilter",urlPatterns = {"/main.jsp", "/class.jsp", "/student.jsp","/delError.jsp"})
/**
 * 过滤器
 */
public class UserFilter implements Filter {
    public void init(FilterConfig config) throws ServletException {
    }

    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        Object userName = httpServletRequest.getSession().getAttribute("login");
        //判断session中是否有保存的登陆信息
        if (null == userName) {
            request.getRequestDispatcher("login.jsp").forward(request, response);
        } else {
            chain.doFilter(request, response);
        }

    }
}
